package com.flight.trans.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.base.BaseRecyclerAdapter;
import com.flight.trans.base.BaseViewHolder;
import com.flight.trans.bean.ErrorRouteBean;
import com.flight.trans.bean.LeavePortDuplicateDetailBean.DuplicateDetailData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.util.CommonUtils;

import java.util.List;

/**
 * 复重详情
 */
public class LeavePortDuplicateDetailsAdapter extends BaseRecyclerAdapter<DuplicateDetailData> {

    public LeavePortDuplicateDetailsAdapter(Context mContext, List<DuplicateDetailData> mList ,boolean isEditor) {
        super(mContext, mList);
        IsEditor = isEditor ;
    }

    @Override
    public int setContentView() {
        return R.layout.adapter_leave_port_duplicate_details;
    }

    @Override
    protected void bindData(BaseViewHolder holder, DuplicateDetailData data, int position) {
        LinearLayout mLlPalletWeight = holder.findViewById(R.id.mLlPalletWeight);
        TextView mTvPalletWeight = holder.findViewById(R.id.mTvPalletWeight);

        TextView mTvContainerNo = holder.findViewById(R.id.mTvContainerNo);
        TextView mTvCount = holder.findViewById(R.id.mTvCount);
        TextView mTvWeight = holder.findViewById(R.id.mTvWeight);

        TextView mTvDuplicateContainerNo = holder.findViewById(R.id.mTvDuplicateContainerNo);
        TextView mTvDuplicateCount = holder.findViewById(R.id.mTvDuplicateCount);
        TextView mTvDuplicateWeight = holder.findViewById(R.id.mTvDuplicateWeight);

        TextView mTvGrossWeight = holder.findViewById(R.id.mTvGrossWeight);
        TextView mTvContainerWeightText = holder.findViewById(R.id.mTvContainerWeightText);
        TextView mTvContainerWeight = holder.findViewById(R.id.mTvContainerWeight);
        TextView mTvNetWeight = holder.findViewById(R.id.mTvNetWeight);
        TextView mTvErrorRateText = holder.findViewById(R.id.mTvErrorRateText);
        TextView mTvErrorRate = holder.findViewById(R.id.mTvErrorRate);
        LinearLayout mLlRefreshTransportWeight = holder.findViewById(R.id.mLlRefreshTransportWeight);

        ImageButton mIbBridgeWeigh = holder.findViewById(R.id.mIbBridgeWeigh);
        ImageView mIvRefresh = holder.findViewById(R.id.mIvRefresh);

        mTvContainerNo.setText("# " + data.getTransportCode());
        mTvCount.setText(String.valueOf(data.getNumber()));
        mTvWeight.setText(String.valueOf(data.getWeight()));

        mTvDuplicateContainerNo.setText("# " + data.getTransportCode());
        mTvDuplicateCount.setText(String.valueOf(data.getNumber()));
        mTvGrossWeight.setText(String.valueOf(data.getWeight() + data.getTransportWeight() + data.getPalletWeight()));

        mTvDuplicateWeight.setText(String.valueOf(data.getDifferenceWeight()));

        mTvContainerWeight.setText(String.valueOf(data.getTransportWeight()));

        double netWeight = data.getDifferenceWeight() - (data.getTransportWeight() + data.getPalletWeight());
        mTvNetWeight.setText(String.valueOf(netWeight));

        mTvContainerWeightText.setText(mContext.getString(data.getTransportType() == 2 ? R.string.text_container_weight : R.string.text_tug_weight));
        mLlPalletWeight.setVisibility(data.getTransportType() == 2 ? View.VISIBLE : View.GONE);
        mTvPalletWeight.setText(String.valueOf(data.getPalletWeight()));

        ErrorRouteBean errorRate = CommonUtils.getErrorRoute(data.getDifferenceWeight() , data.getTransportWeight() + data.getPalletWeight(), data.getWeight()) ;

        mTvErrorRateText.setText(mContext.getString(R.string.text_error_rate));
        mTvErrorRate.setText(CommonUtils.getDouble(errorRate.getErrorRoute() * 100) + " %");

        mTvErrorRate.setTextColor(mContext.getColor(errorRate.isError() ? R.color.color_red : R.color.text_color));
        mTvNetWeight.setTextColor(mContext.getColor(netWeight < 0 || errorRate.isError() ? R.color.color_red : R.color.text_color));

        mTvDuplicateWeight.setBackgroundResource(IsEditor && (ConstantInfo.mPermissionsList.contains(Constant.CGFZ_MODIFY) || ConstantInfo.IS_ADMIN) ? R.drawable.selector_line_trans_shape : R.color.color_cyan);

        mIbBridgeWeigh.setVisibility(IsEditor ? View.VISIBLE : View.GONE);
        mIvRefresh.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

        if(IsEditor) {
            if(ConstantInfo.mPermissionsList.contains(Constant.CGFZ_MODIFY) || ConstantInfo.IS_ADMIN)
                setOnTextClickListener(mTvDuplicateWeight, position);
            setOnTextClickListener(mLlRefreshTransportWeight , position);
            setOnTextClickListener(mIbBridgeWeigh , position);
        }
    }
}
